/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practice.basic;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 * Họ tên Username Password Giới tính : radio Nghê nghiệp : Giáo viên,Kĩ sư,Game thủ,viễn thông Tổng kết : TextAea Đăng
 * kí button -> Message Dialog ghi lại đầy đủ thôn tin Reset button Button Exit,khi click Exit,hoặc click nút Close thì
 * hiện hộp thoại xác nhận
 *
 *
 * @author Bill
 */
public class Basic extends javax.swing.JFrame {

    Timer timer;

    /**
     * Creates new form Basic
     */
    public Basic() {
        initComponents();

        pgbSubmit.setVisible(false);
        lblAge.setText(Integer.toString(sldAge.getValue()));
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jRadioButton4 = new javax.swing.JRadioButton();
        jCheckBox5 = new javax.swing.JCheckBox();
        jgroup = new javax.swing.ButtonGroup();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtFullName = new javax.swing.JTextField();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        rdbMale = new javax.swing.JRadioButton();
        rdbFemale = new javax.swing.JRadioButton();
        rdbOther = new javax.swing.JRadioButton();
        butdk = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        exitbutton = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaMessage = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        chkOption1 = new javax.swing.JCheckBox();
        chkOption2 = new javax.swing.JCheckBox();
        chkOption3 = new javax.swing.JCheckBox();
        jLabel10 = new javax.swing.JLabel();
        sldAge = new javax.swing.JSlider();
        lblAge = new javax.swing.JLabel();
        pgbSubmit = new javax.swing.JProgressBar();

        jLabel6.setText("jLabel6");

        jRadioButton4.setText("jRadioButton4");

        jCheckBox5.setText("jCheckBox5");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Họ tên :");

        jLabel2.setText("UserName :");

        jLabel3.setText("Password :");

        jLabel4.setText("Giới tính :");

        jLabel5.setText("Nghề Nghiệp :");

        jgroup.add(rdbMale);
        rdbMale.setText("Nam");

        jgroup.add(rdbFemale);
        rdbFemale.setText("Nữ");

        jgroup.add(rdbOther);
        rdbOther.setSelected(true);
        rdbOther.setText("Không xác định");
        rdbOther.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbOtherActionPerformed(evt);
            }
        });

        butdk.setText("Đăng Kí");
        butdk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butdkActionPerformed(evt);
            }
        });

        jButton2.setText("Reset");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Game", "Việc Làm", "Buôn Bán" }));

        jLabel7.setText("Đăng kí tại box");

        exitbutton.setText("Exit");
        exitbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitbuttonActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Form đăng kí thử nghiệm");

        txaMessage.setColumns(20);
        txaMessage.setRows(5);
        jScrollPane1.setViewportView(txaMessage);

        jLabel9.setText("Thông Tin Xuất Ra :");

        chkOption1.setText("Thợ điện");

        chkOption2.setText("Game thủ");

        chkOption3.setText("Nội trợ");

        jLabel10.setText("Tuổi :");

        sldAge.setMajorTickSpacing(20);
        sldAge.setMaximum(78);
        sldAge.setMinimum(18);
        sldAge.setMinorTickSpacing(2);
        sldAge.setPaintLabels(true);
        sldAge.setPaintTicks(true);
        sldAge.setValue(18);
        sldAge.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldAgeStateChanged(evt);
            }
        });

        pgbSubmit.setStringPainted(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pgbSubmit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel10)
                                    .addComponent(jLabel5))
                                .addGap(1, 1, 1)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtPassword, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtUsername, javax.swing.GroupLayout.Alignment.LEADING))
                            .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(rdbMale)
                                .addGap(18, 18, 18)
                                .addComponent(rdbFemale)
                                .addGap(18, 18, 18)
                                .addComponent(rdbOther))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(67, 67, 67)
                                .addComponent(jLabel8))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(sldAge, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(chkOption1)
                                        .addGap(18, 18, 18)
                                        .addComponent(chkOption2)
                                        .addGap(18, 18, 18)
                                        .addComponent(chkOption3)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblAge))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(92, 92, 92)
                        .addComponent(butdk, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(exitbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel9)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtFullName, txtUsername});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {butdk, exitbutton, jButton2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(rdbMale)
                    .addComponent(rdbFemale)
                    .addComponent(rdbOther))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(chkOption1)
                    .addComponent(chkOption2)
                    .addComponent(chkOption3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(sldAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblAge)))
                .addGap(18, 18, 18)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(butdk)
                    .addComponent(jButton2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(exitbutton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pgbSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void butdkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butdkActionPerformed
        // Click Dang ky
        String hoten, name;
        hoten = txtFullName.getText();
        name = txtUsername.getText();

        txaMessage.append("Họ tên : " + hoten + "\n");
        txaMessage.append("UserName : " + name + "\n");
        txaMessage.append("Giới tính : ");

        if (rdbMale.isSelected()) {
            txaMessage.append(rdbMale.getText());
        } else if (rdbFemale.isSelected()) {
            txaMessage.append(rdbFemale.getText());
        } else if (rdbOther.isSelected()) {
            txaMessage.append(rdbOther.getText());
        }
        txaMessage.append(System.lineSeparator());

        javax.swing.JCheckBox[] job = {
            chkOption1, chkOption2, chkOption3};

        txaMessage.append("Nghề nghiệp : ");
        for (javax.swing.JCheckBox chk : job) {
            if (chk.isSelected()) {
                txaMessage.append(chk.getText() + ", ");
            }
        }

        txaMessage.append(System.lineSeparator());
        txaMessage.append("Tuổi : " + sldAge.getValue());

        // Insert xuống db
        pgbSubmit.setVisible(true);
        timer = new Timer(200, new ActionListener() {
            int step = 2;

            @Override
            public void actionPerformed(ActionEvent e) {
                pgbSubmit.setValue(pgbSubmit.getValue() + step);
                if (pgbSubmit.getValue() >= 100) {
                    timer.stop();
                    pgbSubmit.setValue(0);
                    pgbSubmit.setVisible(false);
                }
            }
        });

//        timer.setInitialDelay(3000);
        timer.start();
    }//GEN-LAST:event_butdkActionPerformed

    private void exitbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitbuttonActionPerformed
        // TODO add your handling code here:
        checkOnExit();
    }//GEN-LAST:event_exitbuttonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        checkOnExit();
    }//GEN-LAST:event_formWindowClosing

    private void rdbOtherActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbOtherActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rdbOtherActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        txtFullName.setText("");
        txtUsername.setText("");
        txtPassword.setText("");

        rdbMale.setSelected(true);

        javax.swing.JCheckBox[] job = {
            chkOption1, chkOption2, chkOption3};

        for (javax.swing.JCheckBox chk : job) {
            chk.setSelected(false);
        }
        txaMessage.setText("");
    }//GEN-LAST:event_jButton2ActionPerformed

    private void sldAgeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldAgeStateChanged
        // TODO add your handling code here:
        lblAge.setText(Integer.toString(sldAge.getValue()));
    }//GEN-LAST:event_sldAgeStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Windows look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Windows (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Basic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Basic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Basic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Basic.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Basic().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butdk;
    private javax.swing.JCheckBox chkOption1;
    private javax.swing.JCheckBox chkOption2;
    private javax.swing.JCheckBox chkOption3;
    private javax.swing.JButton exitbutton;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.ButtonGroup jgroup;
    private javax.swing.JLabel lblAge;
    private javax.swing.JProgressBar pgbSubmit;
    private javax.swing.JRadioButton rdbFemale;
    private javax.swing.JRadioButton rdbMale;
    private javax.swing.JRadioButton rdbOther;
    private javax.swing.JSlider sldAge;
    private javax.swing.JTextArea txaMessage;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

    private void checkOnExit() throws HeadlessException {
        // TODO add your handling code here:

        if (JOptionPane.showConfirmDialog(this,
                "Bạn có chắc chắn muốn thoát?",
                "Warming", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            System.exit(1);
        }
    }
}
